package com.youyun56.gps.android;

import java.util.ArrayList;
import java.util.List;

import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.youyun56.gps.android.pojo.City;
import com.youyun56.gps.android.pojo.Province;
import com.youyun56.gps.android.pojo.SearchObject;
import com.youyun56.gps.android.service.LocationUploadService;

public class SearchGoodExpandableList extends BaseExpandableListAdapter {
    private String[]            groups = new String[] {"出发，到达城市", "货物特征", "车辆特征" };
    private GoodInfoActivity    activity;
    private View[][]            child;
    private Spinner             fromProvince;
    private Spinner             toProvince;
    private Spinner             fromCity;
    private Spinner             toCity;
    private Button              btnFromCity;
    private Button              btnToCity;
    private ArrayAdapter<City>  fromCityAdapter;
    private ArrayAdapter<City>  toCityAdapter;
    private final static String tag    = SearchGoodExpandableList.class.getName();
    private Spinner             hearvyType, carType;
    private Spinner             hearvyUnin;
    private EditText            hearvyFrom, hearvyEnd;
    private EditText            miFrom, miEnd;
    private String              any    = "任意";
    
    public SearchGoodExpandableList(GoodInfoActivity context) {
        this.activity = context;
        child = new View[][] { {View.inflate(context, R.layout.searchgood_city, null) }, {View.inflate(context, R.layout.searchgood_good, null) }, {View.inflate(context, R.layout.searchgood_car, null) } };
        initComponent();
        init();
        bindEvents();
    }
    
    private void initComponent() {
        View cityItem = child[0][0];
        View goodItem = child[1][0];
        View carItem = child[2][0];
        fromProvince = (Spinner) cityItem.findViewById(R.id.fromProvince);
        fromCity = (Spinner) cityItem.findViewById(R.id.fromCity);
        toProvince = (Spinner) cityItem.findViewById(R.id.toProvince);
        toCity = (Spinner) cityItem.findViewById(R.id.toCity);
        btnFromCity = (Button) cityItem.findViewById(R.id.btnFromCity);
        btnToCity = (Button) cityItem.findViewById(R.id.btnToCity);
        
        hearvyType = (Spinner) goodItem.findViewById(R.id.hearvyType);
        hearvyUnin = (Spinner) goodItem.findViewById(R.id.hearvyUnin);
        hearvyFrom = (EditText) goodItem.findViewById(R.id.hearvyFrom);
        hearvyEnd = (EditText) goodItem.findViewById(R.id.hearvyEnd);
        
        carType = (Spinner) carItem.findViewById(R.id.carType);
        miFrom = (EditText) carItem.findViewById(R.id.miFrom);
        miEnd = (EditText) carItem.findViewById(R.id.miEnd);
        
    }
    
    private void init() {
        List<Province> provinces = new ArrayList<Province>();
        provinces.add(new Province(any));
        provinces.addAll(activity.getProvinces());
        ArrayAdapter<Province> fromProvinceAdapter = new ArrayAdapter<Province>(activity, android.R.layout.simple_spinner_item, provinces);
        fromProvinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fromProvince.setAdapter(fromProvinceAdapter);
        fromProvince.setSelection(activity.getProvinceSpiner().getSelectedItemPosition() + 1);
        fromProvinceAdapter.notifyDataSetChanged();
        List<City> fromCitys = new ArrayList<City>();
        fromCitys.addAll(activity.getCitys());
        fromCityAdapter = new ArrayAdapter<City>(activity, android.R.layout.simple_spinner_item, fromCitys);
        fromCityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fromCity.setAdapter(fromCityAdapter);
        fromCity.setSelection(activity.getCitySpiner().getSelectedItemPosition());
        fromCityAdapter.notifyDataSetChanged();
        
        ArrayAdapter<Province> toProvinceAdapter = new ArrayAdapter<Province>(activity, android.R.layout.simple_spinner_item, provinces);
        toProvinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        toProvince.setAdapter(toProvinceAdapter);
        toProvince.setSelection(activity.getProvinceSpiner().getSelectedItemPosition() + 1);
        toProvinceAdapter.notifyDataSetChanged();
        
        List<City> toCitys = new ArrayList<City>();
        toCitys.addAll(activity.getCitys());
        toCityAdapter = new ArrayAdapter<City>(activity, android.R.layout.simple_spinner_item, toCitys);
        toCityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        toCity.setAdapter(toCityAdapter);
        toCity.setSelection(activity.getCitySpiner().getSelectedItemPosition());
        toCityAdapter.notifyDataSetChanged();
        List<City> citys = activity.getProvinceCity().get(LocationUploadService.getInstance().getCar().getProvince());
        for (City city : citys) {
            if (city.getId().equals(LocationUploadService.getInstance().getCar().getCityId())) {
                String set = "设为" + city.getName();
                btnFromCity.setText(set);
                btnToCity.setText(set);
            }
        }
        
        ArrayAdapter<String> hearvyTypeAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, activity.getResources().getStringArray(R.array.hearvyType));
        hearvyTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hearvyType.setAdapter(hearvyTypeAdapter);
        hearvyTypeAdapter.notifyDataSetChanged();
        ArrayAdapter<String> hearvyUninAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, activity.getResources().getStringArray(R.array.hearvyUnin));
        hearvyUninAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hearvyUnin.setAdapter(hearvyUninAdapter);
        hearvyUninAdapter.notifyDataSetChanged();
        
        ArrayAdapter<String> carTypeAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, activity.getResources().getStringArray(R.array.carType));
        carTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        carType.setAdapter(carTypeAdapter);
        carTypeAdapter.notifyDataSetChanged();
    }
    
    private void bindEvents() {
        // TODO Auto-generated method stub
        fromProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Province province = (Province) fromProvince.getSelectedItem();
                fromCityAdapter.clear();
                if (province.getId() == null) {
                    return;
                }
                fromCityAdapter.add(new City(activity.getResources().getString(R.string.any)));
                List<City> citys = activity.getProvinceCity().get(province.getId());
                for (City city : citys) {
                    fromCityAdapter.add(city);
                }
                // fromCity.setSelection(0);
                fromCityAdapter.notifyDataSetChanged();
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
        
        toProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Province province = (Province) toProvince.getSelectedItem();
                toCityAdapter.clear();
                if (province.getId() == null) {
                    return;
                }
                toCityAdapter.add(new City(activity.getResources().getString(R.string.any)));
                List<City> citys = activity.getProvinceCity().get(province.getId());
                for (City city : citys) {
                    toCityAdapter.add(city);
                }
                toCityAdapter.notifyDataSetChanged();
                // toCity.setSelection(0);
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
        
        btnFromCity.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                fromProvince.setSelection(activity.getProvinceSpiner().getSelectedItemPosition());
                fromCityAdapter.clear();
                for (City city : activity.getCitys()) {
                    fromCityAdapter.add(city);
                }
                fromCityAdapter.notifyDataSetChanged();
                fromCity.setSelection(activity.getCitySpiner().getSelectedItemPosition());
            }
        });
        
        btnToCity.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                toProvince.setSelection(activity.getProvinceSpiner().getSelectedItemPosition());
                toCityAdapter.clear();
                for (City city : activity.getCitys()) {
                    toCityAdapter.add(city);
                }
                toCityAdapter.notifyDataSetChanged();
                toCity.setSelection(activity.getCitySpiner().getSelectedItemPosition());
            }
        });
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child[groupPosition][childPosition];
    }
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }
    
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = (View) getChild(groupPosition, childPosition);
        if (groupPosition == 0) {
            Button btnFromCity = (Button) view.findViewById(R.id.btnFromCity);
            Button btnToCity = (Button) view.findViewById(R.id.btnToCity);
            
        }
        return view;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return child[groupPosition].length;
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groups[groupPosition];
    }
    
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groups.length;
    }
    
    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }
    
    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }
    
    private TextView getGenericView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 60);
        TextView textView = new TextView(activity);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(36, 0, 0, 0);
        textView.setTextAppearance(activity, android.R.attr.textAppearanceLarge);
        return textView;
    }
    
    public String[] getGroups() {
        return groups;
    }
    
    public void setGroups(String[] groups) {
        this.groups = groups;
    }
    
    public GoodInfoActivity getActivity() {
        return activity;
    }
    
    public void setActivity(GoodInfoActivity activity) {
        this.activity = activity;
    }
    
    public View[][] getChild() {
        return child;
    }
    
    public void setChild(View[][] child) {
        this.child = child;
    }
    
    public Spinner getFromProvince() {
        return fromProvince;
    }
    
    public void setFromProvince(Spinner fromProvince) {
        this.fromProvince = fromProvince;
    }
    
    public Spinner getToProvince() {
        return toProvince;
    }
    
    public void setToProvince(Spinner toProvince) {
        this.toProvince = toProvince;
    }
    
    public Spinner getFromCity() {
        return fromCity;
    }
    
    public void setFromCity(Spinner fromCity) {
        this.fromCity = fromCity;
    }
    
    public Spinner getToCity() {
        return toCity;
    }
    
    public void setToCity(Spinner toCity) {
        this.toCity = toCity;
    }
    
    public Button getBtnFromCity() {
        return btnFromCity;
    }
    
    public void setBtnFromCity(Button btnFromCity) {
        this.btnFromCity = btnFromCity;
    }
    
    public Button getBtnToCity() {
        return btnToCity;
    }
    
    public void setBtnToCity(Button btnToCity) {
        this.btnToCity = btnToCity;
    }
    
    public ArrayAdapter<City> getFromCityAdapter() {
        return fromCityAdapter;
    }
    
    public void setFromCityAdapter(ArrayAdapter<City> fromCityAdapter) {
        this.fromCityAdapter = fromCityAdapter;
    }
    
    public ArrayAdapter<City> getToCityAdapter() {
        return toCityAdapter;
    }
    
    public void setToCityAdapter(ArrayAdapter<City> toCityAdapter) {
        this.toCityAdapter = toCityAdapter;
    }
    
    public Spinner getHearvyType() {
        return hearvyType;
    }
    
    public void setHearvyType(Spinner hearvyType) {
        this.hearvyType = hearvyType;
    }
    
    public Spinner getCarType() {
        return carType;
    }
    
    public void setCarType(Spinner carType) {
        this.carType = carType;
    }
    
    public Spinner getHearvyUnin() {
        return hearvyUnin;
    }
    
    public void setHearvyUnin(Spinner hearvyUnin) {
        this.hearvyUnin = hearvyUnin;
    }
    
    public EditText getHearvyFrom() {
        return hearvyFrom;
    }
    
    public void setHearvyFrom(EditText hearvyFrom) {
        this.hearvyFrom = hearvyFrom;
    }
    
    public EditText getHearvyEnd() {
        return hearvyEnd;
    }
    
    public void setHearvyEnd(EditText hearvyEnd) {
        this.hearvyEnd = hearvyEnd;
    }
    
    public EditText getMiFrom() {
        return miFrom;
    }
    
    public void setMiFrom(EditText miFrom) {
        this.miFrom = miFrom;
    }
    
    public EditText getMiEnd() {
        return miEnd;
    }
    
    public void setMiEnd(EditText miEnd) {
        this.miEnd = miEnd;
    }
    
    public SearchObject getSearchObject() {
        SearchObject so = new SearchObject();
        City fromCity = (City) this.getFromCity().getSelectedItem();
        City toCity = (City) this.getToCity().getSelectedItem();
        City targetCity = (City) activity.getCitySpiner().getSelectedItem();
        String hearvyType = (String) this.getHearvyType().getSelectedItem();
        String hearvyUnin = (String) this.getHearvyUnin().getSelectedItem();
        String hearvyFrom = this.getHearvyFrom().getText().toString();
        String hearvyEnd = this.getHearvyEnd().getText().toString();
        String miEnd = this.getMiEnd().getText().toString();
        String miFrom = this.getMiFrom().getText().toString();
        String carType = (String) this.getCarType().getSelectedItem();
        so.setFromCity(fromCity != null && fromCity.getId() != null ? fromCity.getId() : -1);
        so.setToCity(toCity != null && toCity.getId() != null ? toCity.getId() : -1);
        so.setTargetCity(targetCity.getId());
        if (!hearvyType.equals(any)) {
            so.setGsHearvyType(hearvyType);
        }
        if (!hearvyType.equals(any)) {
            so.setGsHearvyUnin(hearvyUnin);
        }
        if (hearvyFrom != null && hearvyFrom.trim().length() > 0) {
            so.setHearvyFrom(Float.valueOf(hearvyFrom));
        }
        if (hearvyEnd != null && hearvyEnd.trim().length() > 0) {
            so.setHearvyEnd(Float.valueOf(hearvyEnd));
        }
        if (miFrom != null && miFrom.trim().length() > 0) {
            so.setGsFromMi(Float.valueOf(miFrom));
        }
        if (miEnd != null && miEnd.trim().length() > 0) {
            so.setGsEndMi(Float.valueOf(miEnd));
        }
        if (!hearvyType.equals(any)) {
            so.setGsCarType(carType);
        }
        return so;
    }
}
